package com.rede.didiok.admin.restapi.admin;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.rede.didiok.admin.annotion.AuthorityVerify.AuthorityVerify;
import com.rede.didiok.admin.annotion.OperationLogger.OperationLogger;
import com.rede.didiok.admin.support.UserSupport;
import com.rede.didiok.common.dto.user.UserDto;
import com.rede.didiok.common.exception.ThrowableUtils;
import com.rede.didiok.common.feign.UserFeignClient;
import com.rede.didiok.common.validator.group.Delete;
import com.rede.didiok.common.validator.group.GetList;
import com.rede.didiok.common.validator.group.Insert;
import com.rede.didiok.common.validator.group.Update;

/**
 * 用户表 RestApi
 *
 * @author Rede
 * @date 2023年10月4日21:29:09
 */
@RestController
@Api(value = "用户相关接口", tags = {"用户相关接口"})
@RequestMapping("/admin/user")
@Slf4j
public class UserRestApi {

    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    private UserSupport userSupport;

    @AuthorityVerify
    @ApiOperation(value = "获取用户列表", notes = "获取用户列表", response = String.class)
    @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("获取用户列表: {}", userDto);
        return userSupport.getUserPageList(userDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "新增用户")
    @ApiOperation(value = "新增用户", notes = "新增用户", response = String.class)
    @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("新增用户: {}", userDto);
        return userSupport.addUser(userDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "编辑用户")
    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = String.class)
    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody UserDto userDto, BindingResult result) {
        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("编辑用户: {}", userDto);
        return userFeignClient.editUser(userDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "删除用户")
    @ApiOperation(value = "删除用户", notes = "删除用户", response = String.class)
    @PostMapping("/delete")
    public String delete(@Validated({Delete.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("删除用户: {}", userDto);
        return userFeignClient.deleteUser(userDto);
    }

    @AuthorityVerify
    @OperationLogger(value = "重置用户密码")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码", response = String.class)
    @PostMapping("/resetUserPassword")
    public String resetUserPassword(@Validated({Delete.class}) @RequestBody UserDto userDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        log.info("重置用户密码: {}", userDto);
        return userSupport.resetUserPassword(userDto);
    }

//    @AuthorityVerify
//    @OperationLogger(value = "用户积分刷数")
//    @ApiOperation(value = "用户积分刷数", notes = "用户积分刷数", response = String.class)
//    @PostMapping("/flushUserCredits")
//    public String flushUserCredits() {
//        return userService.flushUserCredits();
//    }


    /**
     * 目前引入第三方账号 t_user_account 表，需要将原有的用户信息迁移到这里，从而支持多账号绑定
     *
     * @return
     */
    @OperationLogger(value = "用户第三方账号刷数")
    @ApiOperation(value = "用户第三方账号刷数", notes = "用户第三方账号刷数", response = String.class)
    @PostMapping("/flushUserAccount")
    public String flushUserAccount() {
        return userFeignClient.flushUserAccount();
    }


}
